/**  
 * 文 件 名: RightTypeDAO.java  
 * 描    述：   
 * 版    权：Copyright (c)20012-2012 tongzhou All Rights Reserved.
 * 公    司: 同舟 
 * 作    者：彭定雄                     
 * 版    本: 1.0  
 * 创建时间: 2012-10-27
 *  
 * 修改历史：  
 * 时间                             作者                       版本                        描述  
 * ------------------------------------------------------------------  
 * 2012-10-27        彭定雄                1.0          1.0 Version  
 */         

/**
 * 
 */
package com.tongzhou.system.rightsMgmt.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.tongzhou.db.DBUtil;
import com.tongzhou.db.exception.DBException;
import com.tongzhou.system.rightsMgmt.dao.IRightTypeDAO;
import com.tongzhou.system.rightsMgmt.model.RightType;
import com.tongzhou.system.rightsMgmt.model.SearchRightTypeCondition;
import com.tongzhou.util.Util;

public class RightTypeDAO implements IRightTypeDAO{

	@Override
	public void addRightType(RightType rightType) throws DBException {
		StringBuffer sql=new StringBuffer();
		sql.append("insert into sys_right_type(name,table_name,create_by,create_time,update_by,update_time) ");
		sql.append("values('"+rightType.getName()+"','"+rightType.getTable_name()+"',");
		sql.append("'"+rightType.getCreate_by()+"',now(),");
		sql.append("'"+rightType.getCreate_by()+"',now())");
		DBUtil.update(sql.toString());
	}

	@Override
	public List<RightType> checkRightTypeByName(RightType rightType)
			throws DBException {
		StringBuffer sql=new StringBuffer();
		sql.append("select name from sys_right_type where name='"+rightType.getName()+"' and id!='"+rightType.getId()+"'");
		List<RightType> result=DBUtil.query(sql.toString(),new BeanListHandler<RightType>(RightType.class));
		return result;
	}

	@Override
	public List<RightType> checkRightTypeByTableName(RightType rightType)
			throws DBException {
		StringBuffer sql=new StringBuffer();
		sql.append("select name from sys_right_type where table_name='"+rightType.getTable_name()+"' and id!='"+rightType.getId()+"'");
		List<RightType> result=DBUtil.query(sql.toString(),new BeanListHandler<RightType>(RightType.class));
		return result;
	}

	@Override
	public void deleteRightType(RightType rightType) throws DBException {
		StringBuffer sql=new StringBuffer();
		sql.append("delete from sys_right_type where id='"+rightType.getId()+"'");
		DBUtil.update(sql.toString());
	}

//	@Override
//	public List<RightType> getAllRightType() throws DBException {
//		StringBuffer sql=new StringBuffer();
//		sql.append("select r.id,r.name,r.table_name ");
//		sql.append(" from sys_right_type r ");
//		sql.append(" where 1=1 ");
//		List<RightType> result=DBUtil.query(sql.toString(),new BeanListHandler<RightType>(RightType.class));
//		return result;
//	}

	@Override
	public RightType getRightTypeById(RightType rightType) throws DBException {
		StringBuffer sql=new StringBuffer();
		sql.append("select r.id,r.name,r.table_name,r.create_time,r.create_by,r.update_time,r.update_by ");
		sql.append(" from sys_right_type r ");
		sql.append(" where 1=1 ");
		sql.append(" and r.id='"+rightType.getId()+"'");
		RightType rightType1=DBUtil.query(sql.toString(), new BeanHandler<RightType>(RightType.class));
		return rightType1;
	}

	@Override
	public List<RightType> listRightType(SearchRightTypeCondition condition)
			throws DBException {
		List<RightType> result=null;
		//查询条件SQL
		//查询参数
		StringBuffer sqlCondition = new StringBuffer() ;
		List<String> paramList = new ArrayList<String>() ;
		
		if(!Util.checkObjIsNull(condition.getName())){
			sqlCondition.append(" and r.name like ?") ;
			paramList.add("%"+condition.getName()+"%") ;
		}
		if(!Util.checkObjIsNull(condition.getTable_name())){
			sqlCondition.append(" and r.table_name like ?");
			paramList.add("%"+condition.getTable_name()+"%");
		}
		
		//取得满足条件的总记录数和总页数
		StringBuffer sql = new StringBuffer() ;
		sql.append("select count(*) from sys_right_type r ");
		sql.append(" where 1=1 ");
		sql.append(sqlCondition);
		
		//总记录数
		long total = DBUtil.getCount(sql.toString(),paramList.toArray()) ;
		//总页数
		long totalPage = Util.getTotalPage(total, condition.getPageSize());
		
		condition.setTotal(total);
		condition.setTotalPage(totalPage);
		
		//查询某页的列表数据
		sql.delete(0, sql.length());
		sql.append("select r.id,r.name,r.table_name,r.create_by,r.create_time ");
		sql.append(" from sys_right_type r ");
		sql.append(" where 1=1 ");
		sql.append(sqlCondition);
		sql.append(" order by "+condition.getOrderBy()+" "+condition.getOrderByType()) ;
		result = DBUtil.query(sql.toString(), new BeanListHandler<RightType>(RightType.class), paramList.toArray(), Util.getStartPos(condition.getCurrentPage(), condition.getPageSize()), condition.getPageSize()) ;
		return result;
	}

	@Override
	public void updateRightType(RightType rightType) throws DBException {
		StringBuffer sql=new StringBuffer();
		sql.append("update sys_right_type set ");
		sql.append("name='"+rightType.getName()+"',table_name='"+rightType.getTable_name()+"',");
		sql.append("update_by='"+rightType.getUpdate_by()+"',update_time=now()");
		sql.append(" where id='"+rightType.getId()+"' ");
		DBUtil.update(sql.toString());
	}

}
